package main

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-playground/validator/v10"
	"prod/api/prod"
	"prod/internal/conf"
	"prod/internal/dao"
	"prod/internal/service"
	"prod/pkg/env"
	"prod/pkg/network"
	"prod/pkg/request_key"
)

func main() {
	ctx := context.Background()
	if e := conf.Conf.Init(ctx); e != nil {
		fmt.Println(e.Error())
		return
	}
	d, _ := dao.New(ctx, conf.Conf.ORM, conf.Conf.Cache, env.Local, conf.Conf.Downstream)
	s := &service.Service{}
	s.SetDao(d)
	ctx = request_key.UpdateContextWithMetaRequestID(ctx)
	req := &prod.SelectionProdListRequest{
		SgId: 43,
		Ids:  []uint64{157},
	}
	var validate = validator.New()
	if err := validate.RegisterValidation(
		"in_enum", network.EnumValidator(conf.Conf.ServiceName)); err != nil {
		fmt.Println(err.Error())
		return
	}
	if err := validate.Struct(req); err != nil {
		fmt.Println(err.Error())
		return
	}
	resp, err := s.SelectionProdsFull(ctx, req)
	if err != nil {
		fmt.Println(err.Error())
	} else {
		body, _ := json.Marshal(resp)
		fmt.Println(string(body))
	}
	d.Close(ctx)
}

//
//func main(){
//	conn, e := grpc.Dial("127.0.0.1:3456", grpc.WithInsecure(), grpc.WithBlock())
//	if e != nil {
//		log.Fatalln(e)
//	}
//	defer func() {
//		_ = conn.Close()
//	}()
//	client := prod.NewProdClient(conn)
//	ctx := context.Background()
//	req := &prod.GetProdTypeListRequest{
//		Token: &prod.WebToken{
//			EmpId: 1,
//			SgId:  33,
//		},
//		PStoreId: 0,
//		Nature: prod.Nature_NATURE_SALE,
//	}
//	if resp, err := client.GetProdTypeList(ctx, req); err != nil {
//		for _, detail := range status.Convert(err).Details() {
//			switch t := detail.(type) {
//			case *prod.DefaultResp:
//				log.Println(t.ErrorCode, t.ErrorMessage)
//				return
//			}
//		}
//		log.Println(err.Error())
//	} else {
//		log.Println(resp.ProdTypes)
//	}
//}
